package com.example.common.ui;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.common.R;
import com.example.common.widget.state.NetworkStateLayout;
import com.example.common.widget.state.NetworkStateView;
import com.example.common.widget.state.OnAnewRequestNetworkListener;

/**
 * Created by admin on 2017/4/11
 * 邮箱: helloxiangsheng@163.com
 * 功能: 带有各种网络状态的baseFragment
 */

public abstract class AbsNetworkStateFragment extends BaseFragment implements OnAnewRequestNetworkListener, NetworkStateView {

    private NetworkStateLayout mStateLayout;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mStateLayout = (NetworkStateLayout) inflater.inflate(
                R.layout.fragment_network_state, container, false);

        mStateLayout.addView(LayoutInflater.from(getContext()).inflate(getLayoutID(),
                mStateLayout, false));

        // 重新请求网络监听
        mStateLayout.setOnAnewRequestNetworkListener(this);
        return mStateLayout;
    }

    @Override
    public void notNetwork() {
        mStateLayout.showNetworkStateView(NetworkStateLayout.NOT_NETWORK_STATE);
    }

    @Override
    public void loadingNetwork() {
        mStateLayout.showNetworkStateView(NetworkStateLayout.LOADING_NETWORK_STATE);
    }

    @Override
    public void successNetwork() {
        mStateLayout.showContentView();
    }

    @Override
    public void emptyNetwork() {
        mStateLayout.showNetworkStateView(NetworkStateLayout.EMPTY_NETWORK_STATE);
    }

    @Override
    public void errorNetwork(CharSequence charSequence) {
        mStateLayout.showNetworkStateView(NetworkStateLayout.ERROR_NETWORK_STATE, charSequence);
    }

    @Override
    public void onAnewRequestNetwork() {
        onAnewRequest();
    }

    /**
     * 重新请求网络
     */
    public abstract void onAnewRequest();
}
